package bTree.LeetCode;

public class 最长回文子序列 {
    public int longestPalindromeSubseq(String s) {
        int length = s.length();
        int[][] dp = new int[length + 1][length + 1];
        for (int i = length; i > 0; i--) {
            for (int j = i; j <= length; j++) {
                if (s.charAt(i - 1) == s.charAt(j - 1)) {
                    if(j-i<=1){
                        dp[i][j]=j-i+1;
                    }else {
                        dp[i][j]=dp[i+1][j-1]+2;
                    }
                }else {
                    dp[i][j]=Math.max(dp[i][j-1],dp[i+1][j]);
                }
            }
        }
        return dp[1][length];
    }
}